/**
 * form 表单验证
 */
class Validation {
    constructor(options = {}) {
        this.options = options;
        this.$form = $(this.options.selector);
        this.validation = {
            digits: {
                default: "输入值不是一个数"
            },
            enCode: {
                regexp: /^[0-9a-zA-Z()]+$/,
                default: "仅支持大小写英文字母、数字、括号"
            },
            numeric: {
                regexp: /^\d+(\.\d{1,2})?$/,
                default: "小数点后数字不超过两位"
            },
            cashnum: {
                regexp: /^[0-9]{1,8}([.][0-9]{1,2})?$/,
                default: "小数点后数字不超过两位且小于1亿"
            },
            integer: {
                regexp: /^([1-9][0-9]*|-[1-9][0-9]*)$/,
                default: "请输入有效整数"
            },
            phone: {
                regexp: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                default: "请输入正确的手机号"
            },
            creditCard: {
                default: "信用卡卡号错误"
            },
            ip: {
                ipv4: true,
                ipv6: true,
                default: "IP格式有误"
            },
            storeNo: {
                regexp: /^[0-9a-zA-Z]+$/,
                default: "仅支持英文字母和数字"
            },
            storePwd: {
                regexp: /^[\da-zA-Z~!@#$%^&*._-]{6,15}$/,
                default: "密码由6-15位半角字符（英文字母、数字、符号~!@#$%^&*._-）组成，区分大小写"
            },
            intCode: {
                regexp: /^[\da-zA-Z~!@#$%^&*._-]+$/,
                default: "仅支持（英文字母、数字、符号~!@#$%^&*._-），区分大小写"
            }
        }
    }

    init() {
        return this.getFields();
    }

    getFields() {
        let self = this;
        var fields = {};
        var inputArr = self.$form.find('[name][data-v]');
        inputArr.each(function (i, input) {
            var validators = {};
            var name = $(input).attr('name');
            fields[name] = {};
            var v = [];
            var data = $(input).data('v');
            if (data.indexOf('regexp') > -1) {
                v = data.split('\\,');
            } else {
                v = data.split(',');
            }
            var m = $(input).data('m').split(',');
            v.forEach(function (validator, index) {
                if (validator.indexOf('notempty') > -1) {
                    validators['notEmpty'] = {
                        "message": m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('len:') > -1) {
                    if (!validators['stringLength']) {
                        validators['stringLength'] = {};
                    }
                    var len = validator.split(':')[1].split('-');
                    validators['stringLength'].min = len[0];
                    validators['stringLength'].max = len[1];
                    validators['stringLength'].message = m[index] || self.getMessage(validator).default;
                } else if (validator.indexOf('number') > -1) {
                    //数字
                    validators['integer'] = {
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('email') > -1) {
                    //emailAddress
                    validators['emailAddress'] = {
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('phone') > -1) {
                    //phone
                    validators['phone'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('cashnum') > -1) {
                    //cashnum
                    validators['cashnum'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('integer') > -1) {
                    //integer正整数
                    validators['integer'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('intCode') > -1) {
                    //intCode
                    validators['intCode'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('numeric') > -1) {
                    //numeric
                    validators['numeric'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('enCode') > -1) {
                    //enCode
                    validators['enCode'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('storeNo') > -1) {
                    //storeNo
                    validators['storeNo'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('storePwd') > -1) {
                    //storePwd
                    validators['storePwd'] = {
                        regexp: self.getMessage(validator).regexp,
                        message: m[index] || self.getMessage(validator).default
                    };
                } else if (validator.indexOf('password') > -1) {
                    //password
                    validators['callback'] = {
                        callback: function callback(value, fv) {
                            if (fv.getStatus('password2', 'notEmpty') == 'INVALID') {
                                return true;
                            };
                            if (fv.getStatus('password2', 'stringLength') == 'INVALID') {
                                return true;
                            };

                            var inputName = validator.split(':').pop();
                            var inputValue = $('[name=' + inputName + ']').val();
                            if (inputValue == value) {
                                return true;
                            } else {
                                return false;
                            }
                        },
                        message: m[index]
                    };
                } else if (validator.indexOf('regexp') > -1) {
                    //正则校验
                    var regStr = validator.split(':').pop();
                    validators['regexp'] = {
                        regexp: new RegExp(regStr),
                        message: m[index]
                    };
                } else {
                    validators[validator] = $.extend({}, self.getMessage(validator))
                    if (m[index]) {
                        validators[validator].message = m[index];
                    }
                }
            });
            fields[name].validators = validators;
        });
        return fields;
    }

    getMessage(validator) {
        var key = validator;
        if (validator.indexOf(':') > -1) {
            key = validator.split(':')[0];
        }
        return this.validation[key];
    }
};